package com.topvision.controller;

import com.topvision.common.response.SuccessResponse;
import com.topvision.common.security.UserInfo;
import com.topvision.common.security.UserInfoUtils;
import com.topvision.domain.SysOrder;
import com.topvision.domain.SysOrderDetail;
import com.topvision.domain.SysProduct;
import com.topvision.dto.OrderDTO;
import com.topvision.dto.ProductSrvDTO;
import com.topvision.service.SysOrderDetailService;
import com.topvision.service.SysOrderService;
import com.topvision.service.SysProductService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order-api")
@Api(value = "订单相关API", description = "用户订单行为")
@Slf4j
public class SysOrderController {

    @Autowired
    private SysOrderService sysOrderService;
    @Autowired
    private SysOrderDetailService sysOrderDetailService;
    @Autowired
    private SysProductService sysProductService;
    @Autowired
    private UserInfoUtils userInfoUtils;

    @PostMapping("/submit")
    public ResponseEntity submit(@RequestBody OrderDTO orderDTO, Principal principal) {
        UserInfo userInfo = userInfoUtils.getUserInfo(principal);
        SysOrder sysOrder = new SysOrder();
        SysOrderDetail sysOrderDetail = new SysOrderDetail();
        Long id = Long.valueOf(orderDTO.getProductId().trim());
        SysProduct sysProduct = sysProductService.findone(id);
        //新订单业务处理
        if (StringUtils.isNotBlank(orderDTO.getEmail())) {
            sysOrder.setEmail(orderDTO.getEmail().trim());
        }
        if (StringUtils.isNotBlank(orderDTO.getMobile())) {
            sysOrder.setMobile(orderDTO.getMobile().trim());
        }
        if (StringUtils.isNotBlank(orderDTO.getUserName())) {
            sysOrder.setUserName(orderDTO.getUserName().trim());
        }
        if (StringUtils.isNotBlank(orderDTO.getImgUrl())) {
            sysOrder.setImgUrl(orderDTO.getImgUrl().trim());
        }
        sysOrder.setUserId(userInfo.getId());
        sysOrder.setOrderStatus(0);
        sysOrderService.save(sysOrder);
        //订单详情
        sysOrderDetail.setProductId(id);
        if (StringUtils.isNotBlank(orderDTO.getProdctName())) {
            sysOrderDetail.setProductName(orderDTO.getProdctName().trim());
        }
        if (sysProduct.getMonNum() != null && sysProduct.getMonNum() > 0) {
            ZonedDateTime zonedDateTime = ZonedDateTime.now();
            ZonedDateTime outdate = zonedDateTime.plus(sysProduct.getMonNum(), ChronoUnit.MONTHS);
            sysOrderDetail.setOutDate(outdate);
        }
        sysOrderDetail.setOrderId(sysOrder.getId());
        sysOrderDetailService.save(sysOrderDetail);
        return ResponseEntity.ok(SuccessResponse.of(sysOrderDetail));
    }

    @PostMapping("/myOrders")
    public ResponseEntity myOrders(Principal principal) {
        UserInfo userInfo = userInfoUtils.getUserInfo(principal);
        List<Long> ids = new ArrayList<>();
        List<SysOrder> sysOrderList = sysOrderService.findByUserId(userInfo.getId());
        Map<Long, SysOrder> map = new HashMap<>();
        sysOrderList.forEach(sysOrder -> {
            ids.add(sysOrder.getId());
            map.put(sysOrder.getId(), sysOrder);
        });
        List<SysOrderDetail> sysOrderDetailList = sysOrderDetailService.findSysOrderDetailsByOrderIdIn(ids);
        List<ProductSrvDTO> productSrvDTOList = new ArrayList<>();
        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        sysOrderDetailList.forEach(sysOrderDetail -> {
            if(sysOrderDetail.getOutDate().compareTo(zonedDateTime) > 0){
                ProductSrvDTO productSrvDTO = new ProductSrvDTO();
                productSrvDTO.setOderId(sysOrderDetail.getOrderId());
                productSrvDTO.setSysOrder(map.get(sysOrderDetail.getOrderId()));
                productSrvDTO.setSysOrderDetail(sysOrderDetail);
                productSrvDTOList.add(productSrvDTO);
            }
        });



        return ResponseEntity.ok(SuccessResponse.of(productSrvDTOList));
    }
}
